package jp.ac.aiit.spg.web.wbs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.EJB;

import org.seasar.teeda.extension.annotation.scope.SubapplicationScope;

import jp.ac.aiit.spg.AbstractSpgPage;
import jp.ac.aiit.spg.common.*;
import jp.ac.aiit.spg.entity.*;
import jp.ac.aiit.spg.service.SpgService;
import jp.ac.aiit.spg.web.login.LoginPage;

public class ListProjectsPage extends AbstractSpgPage {

	@EJB
	private SpgService ss;

	private Collection<SpgProject> projects;

	private List<SpgProjectItem> spgprojectDisplayItems; // この画面用の変数名を直感的にspgprojectItemsとしてはいけない。テーブル名とダブるから。

	@SubapplicationScope
	private int projectid;

	private String projectName;

	private String projectDescription;
	

	// ロジック
	
	public Class initialize() {
		
		// ログインチェック
		if (this.isLoggedIn() == false) {
			return LoginPage.class;
		} else {
			return null;			
		}
	}

	public Class prerender() {

		spgprojectDisplayItems = new ArrayList<SpgProjectItem>();

		// ログインしたユーザーが関係しており、かつPMになっているプロジェクトのみをリストする。
		List<SpgProject> projects = ss.listUserProject(this.getLoginUserId(), SpgRole.PROJECTMANAGER); 

		for (SpgProject p : projects) {
			SpgProjectItem item = new SpgProjectItem();
			item.setProjectId(p.getId());
			item.setProjectName(p.getName());
			item.setProjectDescription(p.getDescription());
			spgprojectDisplayItems.add(item);
		}

		return null;
	}


	


	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public Collection<SpgProject> getProjects() {
		return projects;
	}

	public void setProjects(Collection<SpgProject> projects) {
		this.projects = projects;
	}

	public List<SpgProjectItem> getSpgprojectDisplayItems() {
		return spgprojectDisplayItems;
	}

	public void setSpgprojectDisplayItems(List<SpgProjectItem> spgprojectDisplayItems) {
		this.spgprojectDisplayItems = spgprojectDisplayItems;
	}

	public int getProjectid() {
		return projectid;
	}

	public void setProjectid(int projectid) {
		this.projectid = projectid;
	}

	
	public String getProjectDescription() {
		return projectDescription;
	}

	public void setProjectDescription(String projectDescription) {
		this.projectDescription = projectDescription;
	}

	
}
